package com.yiwenup.struct._03_collect.set;

import com.yiwenup.struct._01_linear.base.List;
import com.yiwenup.struct._01_linear.list.SingleLinkedList;
import com.yiwenup.struct._03_collect.set.base.Set;

/**
 * 链表实现
 **/
public class ListSet<E> implements Set<E> {
    private List<E> list = new SingleLinkedList<E>();

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public boolean contains(E element) {
        return list.contains(element);
    }

    @Override
    public void add(E element) {
        if (list.contains(element)) return;
        list.add(element);
    }

    @Override
    public void remove(E element) {
        list.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if (visitor == null) return;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (visitor.visit(list.get(i))) return;
        }
    }
}
